package com.xxx4biji.date.novel;

import org.junit.Test;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;


public class TestLocalDateTime {
    //ZonedDate、ZonedTime、ZoneDateTime
    @Test
    public void test6() {
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        //zoneIds.forEach(System.out::println); // Asia/Shanghai
        LocalDateTime time = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(time);

        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zone = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zone);
    }

    //DateTimeFormatter:格式化时间/日期
    @Test
    public void test5() throws ParseException {
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
        LocalDateTime now = LocalDateTime.now();
        String date = now.format(formatter);
        System.out.println(date);
        System.out.println("--------------------");

        //日期转字符串
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM日dd日 HH:mm:ss");
        String date1 = format.format(LocalDateTime.now());
        System.out.println(date1);
        System.out.println("-----------------------");

        //字符串转日期
        LocalDateTime time = LocalDateTime.parse(date1, format);
        System.out.println(time);
        System.out.println(new Date());


    }

    //TemporalAdjuster:时间校正器
    @Test
    public void test4() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        //指定日期
        LocalDateTime time2 = dateTime.withDayOfMonth(10);
        System.out.println(time2);

        //下一个周六
        LocalDateTime with = dateTime.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
        System.out.println(with);
    }


    @Test
    public void testCe() {

        //LocalDateTime myDate = LocalDateTime.of(2021, 12, 28, 11, 0, 0);
        //Duration
        LocalDateTime start = LocalDateTime.of(2021, 12, 28, 14, 0, 0);
        LocalDateTime end = LocalDateTime.of(2021, 12, 28, 12, 0, 0);
                //大 小 负
                // 小 大 正
        System.out.println(Duration.between(start, end).getSeconds());
        boolean before = start.isBefore(end);
        System.out.println(before);
        //Period
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.of(2021, 12, 8);
        System.out.println(Period.between(date1, date2).getDays());
    }


    @Test
    public void more(){
        //用于计算两个时间的差值
//        Date date1 = new Date();
//        date1.setTime(1640745513000l);
//        System.out.println(date1);
//        Date date2 = new Date();
//        date2.setTime(1640815200000l);
//        System.out.println(date2);
//        System.out.println(24*60*60*1000);
//        BigDecimal bigDecimal = new BigDecimal(Double.valueOf(date2.getTime()-date1.getTime()));
//        double price = bigDecimal.divide(new BigDecimal(Double.valueOf(86400000)), 2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
//        System.out.println(price);
//
//
//        double v = new BigDecimal((double) (((date2.getTime()-date1.getTime())/86400000))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//        System.out.println((date2.getTime()-date1.getTime()));
//        System.out.println((double) (((date2.getTime()-date1.getTime())/86400000)));
//        System.out.println("v=======>"+v);
//        System.out.println((date2.getTime()-date1.getTime())/86400000);

        LocalDateTime jintian = LocalDateTime.of(2021, 12, 28, 10, 0, 0);
        LocalDateTime yonhu = LocalDateTime.of(2021, 12, 28, 9, 0, 0);
        boolean before = yonhu.isBefore(jintian);
        System.out.println(before);


    }

    /**
     * 判断某个时间段是否在另外时间段
     * @param start
     * @param end
     * @param myDate
     * @return
     */
//    @Test
//    public void isUse(LocalDateTime start,LocalDateTime end,LocalDateTime myDate) {
//
//    }

    //3.计算时间间隔
    //Duration:计算两个"时间"之间的间隔
    //Period:计算两个"日期"之间的间隔
    @Test
    public void test3() {
        //Duration
        LocalDateTime start = LocalDateTime.now();
        LocalDateTime end = start.plusSeconds(600);
        System.out.println(Duration.between(start, end).getSeconds());
        //Period
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.of(2021, 12, 8);
        System.out.println(Period.between(date1, date2).getDays());


    }


    //2.Instant:时间戳(以Unix 元年: 1970年1月1日00:00:00到某个时间之间的毫秒值)
    @Test
    public void test2() {
        Instant date = Instant.now();
        System.out.println("默认获取UTC时区:" + date);//默认获取UTC时区

        OffsetDateTime time = date.atOffset(ZoneOffset.ofHours(8));
        System.out.println(time);

        System.out.println(date.toEpochMilli());


    }


    //1.LocalDate LocalDateTime LocalTime
    @Test
    public void test1() {
        LocalDateTime time = LocalDateTime.now();
        System.out.println(time);

        LocalDateTime date2 = LocalDateTime.of(2015, 11, 20, 19, 33, 20);
        System.out.println(date2);


    }

    @Test
    public void ceshi() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list.get(3));

    }
}
